package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/7/13.<br><br>
 *
 * 第一个错误的版本
 */
public class Topic278 {

    private int badVersion;

    public static void main(String[] args) {
        Topic278 instance = new Topic278();

        instance.badVersion = 1;
        System.out.println(instance.firstBadVersion(1));

        instance.badVersion = 4;
        System.out.println(instance.firstBadVersion(5));

        instance.badVersion = 1702766719;
        System.out.println(instance.firstBadVersion(2126753390));
    }

    public int firstBadVersion(int n) {
        int low = 1;
        int high = n;

        while (low < high) {
            int middle = (low + high) >>> 1;
            if(isBadVersion(middle)) {
                high = middle;
            }
            else {
                low = middle + 1;
            }
        }

        return low;
    }

    private boolean isBadVersion(int n) {
        return n >= badVersion;
    }
}
